package com.shun.media.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shun.common.R;
import com.shun.common.utils.ShunCommonUtils;
import com.shun.media.dto.GjydFilmSceneDto;
import com.shun.media.dto.SceneFileDto;
import com.shun.media.mapper.GjydFilmSceneMapper;
import com.shun.media.service.IGjydFilmSceneService;
import com.shun.media.vo.GjydFilmScene;
import com.shun.oss.service.OssExecuteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 拍摄场景(位点) 服务实现类
 * </p>
 *
 * @author clever
 * @since 2021-10-18
 */
@Slf4j
@Service
public class GjydFilmSceneServiceImpl extends ServiceImpl<GjydFilmSceneMapper, GjydFilmScene> implements IGjydFilmSceneService {

    private final static String ADD_SCENE_ERR = "新增场景信息失败！";

    private final static String UPDATE_SCENE_ERR = "更新场景基础信息失败！";

    private final static String ADD_SCENE_FILE_ERR = "场景新增文件失败！";

    private final static String DEL_SCENE_ERR = "删除场景信息失败！";

    @Autowired
    private GjydFilmSceneMapper sceneMapper;

    @Autowired
    private OssExecuteService ossExecuteService;

    @Override
    public R addScence(GjydFilmSceneDto sceneDto) {
        GjydFilmScene scene = ShunCommonUtils.beanMap(sceneDto, GjydFilmScene.class);
        scene.setSceneId(IdUtil.objectId());
//        scene.set
        return save(scene) ? R.suc(scene.getSceneId()) : R.error(ADD_SCENE_ERR);
    }

    @Override
    public R appendFiles(SceneFileDto fileDto,String fmType) {
        int count = sceneMapper.appendFiles(fileDto.getSceneId(),fmType, fileDto.getIds());
        return count > 0 ? R.suc() : R.error(ADD_SCENE_FILE_ERR);
    }

    @Override
    public R editFiles(SceneFileDto fileDto, String fmType) {
        sceneMapper.delSceneFilm(fileDto.getSceneId(),fmType);
        return appendFiles(fileDto,fmType);
    }

    @Override
    public R updateInfo(GjydFilmSceneDto sceneDto) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("scene_id", sceneDto.getSceneId());
        boolean updateFlg = update(updateWrapper);
        return updateFlg ? R.suc() : R.error(UPDATE_SCENE_ERR);
    }

    @Override
    public R delSceneById(String sceneId) {
        if (removeById(sceneId)) {
            int count = sceneMapper.delSceneFilm(sceneId,null);
            log.debug("删除关联文件数量：{}", count);
            return R.suc();
        }
        return R.error(DEL_SCENE_ERR);
    }

    @Override
    public R pageScene(GjydFilmSceneDto sceneDto) {
        log.debug("查询条件:{}", sceneDto);
        IPage<Map<String, Object>> sceneIPage = sceneMapper.pageScene(sceneDto.toPage(), Wrappers.emptyWrapper());
        List<Map<String, Object>> records = sceneIPage.getRecords();
        List<Map<String, Object>> newRecords = records.stream().
                map(m -> {
                    HashMap<String, Object> newMap = new HashMap<String, Object>();
                    newMap.putAll(m);
                    newMap.put("path", ossExecuteService.getOssFileUrl(m.get("ossPath").toString(), m.get("ossType").toString()));
                    return newMap;
                }).collect(Collectors.toList());
        sceneIPage.setRecords(newRecords);
        log.debug("sceneIPage:{}", JSONUtil.toJsonPrettyStr(sceneIPage));
        return R.suc(sceneIPage);
    }

    @Override
    public R pageSceneFile(SceneFileDto sceneFileDto, String type) {
        IPage<Map<String, Object>> sceneFileIPage = sceneMapper.pageSceneFile(sceneFileDto.toPage(),
                Wrappers.query().eq("gsr.s_id",sceneFileDto.getSceneId()).eq("gsr.rs_type",type));
        List<Map<String, Object>> records = sceneFileIPage.getRecords();
        List<Map<String, Object>> newRecords = records.stream().
                map(m -> {
                    HashMap<String, Object> newMap = new HashMap<String, Object>();
                    newMap.putAll(m);
                    newMap.put("path", ossExecuteService.getOssFileUrl(m.get("ossPath").toString(), m.get("ossType").toString()));
                    return newMap;
                }).collect(Collectors.toList());
        sceneFileIPage.setRecords(newRecords);
        log.debug("sceneIPage:{}", JSONUtil.toJsonPrettyStr(sceneFileIPage));
        return R.suc(sceneFileIPage);
    }
}
